package com.bigdata.mapreduce.compare_;

import org.junit.Test;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {

    @Test
    public void test1() {
        TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                /*
                按照name正序，看compare对象的位置
                    this在前，target在后：正序
                    target在前，this在后：倒序
                    在正序的基础上取反，也可以实现倒序
                 */
                int compare = -s1.getName().compareTo(s2.getName());
                if (compare == 0) {
                    // age正序
                    return -(s1.getAge() - s2.getAge());
                }
                return compare;
            }
        });
        Student s1 = new Student("zs", 22);
        Student s2 = new Student("ls", 26);
        Student s3 = new Student("ww", 28);
        // 如果数据重复了, 会保留已存在的数据
        Student s4 = new Student("zs", 20);


        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        System.out.println(set);
    }

    public static void main(String[] args) {
        Set<Student> set = new TreeSet<>();
        Student s1 = new Student("zs", 22);
        Student s2 = new Student("ls", 26);
        Student s3 = new Student("ww", 28);
        Student s4 = new Student("zs", 20);


        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        System.out.println(set);

    }
}
